from flask import Flask, jsonify, request, abort
from answer_question import get_answer

app = Flask(__name__)

@app.route('/get_mental_health_knowledge', methods=['POST'])
def get_knowledge():
    # 从 POST 请求体中获取 'question' 参数
    question = request.json.get('question')
    if not question:
        abort(400, description="缺少问题参数，请提供问题")

    # 调用 answer_question.py 中的 get_answer 函数获取答案
    answer = get_answer(question)

    # 检查 answer 是否为 None
    if answer is None:
        return jsonify({"errCode": "no_data", "message": "无法匹配到相关知识，请换个问题提问"}), 404

    return jsonify({"answer": answer})

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)